/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */
 
package org.ceroproject.ui.commandline.commands;

import java.util.Collection;

import org.ceroproject.games.Action;
import org.ceroproject.games.ActionEvent;
import org.ceroproject.games.ActionException;
import org.ceroproject.games.ActionListener;
import org.ceroproject.games.Game;
import org.ceroproject.games.Zone;
import org.ceroproject.games.base.ActionLookAtZone;
import org.ceroproject.ui.commandline.Command;
import org.ceroproject.ui.commandline.CommandLineLauncher;


/**
 * @author Roux Camille
 */
public class See extends Command implements ActionListener {

	private static See instance = new See();

	private static ActionLookAtZone currentAction;

	private See() {
		super("see");
	}

	public static See getCommand() {
		return instance;
	}

	@Override
	public String toString() {
		return "see a zone like a player";
	}

	public boolean executer(String parametres) {
		Game game = CommandLineLauncher.getGame();
		if (game == null || parametres.trim() == "")
			return false;
		String[] split = parametres.split(":");
		game.addActionListener(this);

		Collection collection = null;
		if (split[0].equals("listz")) {
			if (split.length == 1)
				collection = game.getZones();
			else {
				collection = game.getPlayers().get(Integer.valueOf(split[1]))
						.getZones();
			}
			print(collection);
		} else if (split[0].equals("z")) {
			if (split.length == 2) {
				Zone zone = CommandLineLauncher.getGame().getZones().get(
						split[1]);
				if (zone == null) {
					System.out.println("zone inconnue");
					return false;
				}
				else {
					ActionLookAtZone action = new ActionLookAtZone();
					try {
						action.playerActed(new ActionEvent(CommandLineLauncher
								.getGame(), CommandLineLauncher.getCurrentPlayer(),
								zone, null, null));
					} catch (ActionException e) {
						e.printStackTrace();
					}
					CommandLineLauncher.getGame().registerAction(action);
					currentAction = action;
				}
			}
			else if (split.length == 3) {
				Zone zone = CommandLineLauncher.getGame().getPlayers().get(
						Integer.valueOf(split[1])).getZones().get(split[2]);
				if (zone == null) {
					System.out.println("zone inconnue");
					return false;
				}
				else {
					ActionLookAtZone action = new ActionLookAtZone();
					currentAction = action;
					try {
						action.playerActed(new ActionEvent(CommandLineLauncher
								.getGame(), CommandLineLauncher.getCurrentPlayer(),
								zone, null, null));
					} catch (ActionException e) {
						e.printStackTrace();
					}
					CommandLineLauncher.getGame().registerAction(action);
				}
			} else {
				System.out.println("bad arguments");
				return false;
			}
		} else {
			System.out.println("bad argument(s)");
			return false;
		}

		return true;
	}

	private static void print(Collection collection) {
		if (collection != null)
			for (Object obj : collection)
				System.out.println(obj.toString());
	}

	public void onPlayerPlayed(Action action) {
		// do nothing
		System.out.println("Player played action "+action.getActionName());
	}

	public void onExecuted(Action action) {
		System.out.println("Cards in the zone "+action.getEvent().getZoneFrom().getName());
		ActionLookAtZone actionLAC = (ActionLookAtZone) action;
		if ((actionLAC).equals(currentAction)) {
			print(actionLAC.getCards());
		}
	}

	public void onFailed(Action action) {
		System.out.println("The action failed");
	}
	
	@Override
	public String help() {
		return "see a zone like a player\n"
				+ "syntax:\n"
				+ "see:listz  :  list the zones of the game\n"
				+ "see:listz:playerbn  :  list the zones of the player number 'playernb'\n"
				+ "see:z:zone  :  list the card of the zone 'zone' of the game visible by the current player\n"
				+ "see:z:playernb:zone  :  list the card of the zone 'zone' of the player number 'playernb' visible by the current player\n";
	}

	public Class listenedAction() {
		return ActionLookAtZone.class;
	}

}
